<template>
  <div class="categories-create-or-edit">
    <el-form inline="true">
      <el-form-item label="名称:">
        <el-input v-model="categories.name" value="id"></el-input>
      </el-form-item>
      <el-form-item label="排序:">
        <el-input  v-model="categories.sort" value="id"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="onCancel">取 消</el-button>
        <el-button type="primary" @click="onSubmit">确 定</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { saveOrderUpdate } from '@/services/resource'
export default {
  name: 'CategoriesCreateOrEdit',
  props: {
    isEdit: {
      type: Boolean,
      default: false
    },
    categoryId: {
      type: [Number, String],
      required: true
    },
    resourcecategories: {
      type: Array,
      default: () => { return [] }
    }
  },
  created () {
    //  加载上级资源分类信息
    this.loadCategory()
  },
  data () {
    return {
      categories: {
        id: '',
        name: '',
        sort: ''
      }
    }
  },
  methods: {
    // 点击确定提交添加资源分类的信息
    async onSubmit () {
      const { data } = await saveOrderUpdate(this.categories)
      // console.log(data)
      if (data.code === '000000') {
        // 如果添加成功 关闭提示框并清除表单
        this.$emit('success')
        this.$message.success('添加资源成功')
        this.categories = {}
      }
    },
    // 点击取消 退出提示框清除内容并且提示取消添加 onCancel
    onCancel () {
      this.$emit('cancel')
      this.$message.warning('已取消')
      this.categories = {}
    },
    loadCategory () {
      console.log(this.categoryId)
      console.log(this.resourcecategories)
      this.resourcecategories.forEach((item, index) => {
        // console.log(item.id)
        if (item.id === this.categoryId) {
          this.categories.sort = item.sort
          this.categories.name = item.name
        }
      })
    }
  }
}

</script>

<style lang="scss" scoped></style>
